package com.kcwx.web.controller.system;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.kcwx.common.annotation.Log;
import com.kcwx.common.core.controller.BaseController;
import com.kcwx.common.core.domain.AjaxResult;
import com.kcwx.common.core.page.TableDataInfo;
import com.kcwx.common.enums.BusinessType;

import com.kcwx.shareddomain.common.dto.DeleteDto;
import com.kcwx.shareddomain.common.dto.DelParaValid;
import com.kcwx.shareddomain.system.query.SysNoticeQuery;
import com.kcwx.shareddomain.system.dto.SysNoticeEditDto;

import com.kcwx.system.domain.SysNotice;
import com.kcwx.system.dtomap.SysNoticeMap;
import com.kcwx.system.service.ISysNoticeService;

/**
 * 公告 信息操作处理
 * 
 */
@Api(tags = "系统管理-通知公告")
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController
{
    @Autowired
    private ISysNoticeService noticeService;

    @Autowired
    private SysNoticeMap sysNoticeMap;

    /**
     * 新增通知公告
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "新增通知公告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysNoticeEditDto dto)
    {
        SysNotice notice=sysNoticeMap.AddDtoToEntity(dto);
        notice.setCreateBy(getUsername());
        int rowsAffect=noticeService.insertNotice(notice);
        return rowsAffect>0?success():error("新增失败");
    }

    /**
     * 修改通知公告
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @Log(title = "修改通知公告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SysNoticeEditDto dto)
    {
        SysNotice notice=sysNoticeMap.EditDtoToEntity(dto);
        notice.setUpdateBy(getUsername());
        int rowsAffect=noticeService.updateNotice(notice);
        return rowsAffect>0?success():error("修改失败");
    }

    /**
     * 删除通知公告
     */
    @ApiOperation("删除通知公告")
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "删除通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestBody DeleteDto dto)
    {
        Long id=DelParaValid.validDel(dto);
        int rowsAffect=noticeService.deleteNoticeById(id,getUsername());
        return rowsAffect>0?success():error("删除失败");
    }

    /**
     * 批量删除通知公告
     */
    @ApiOperation("批量删除通知公告")
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "批量删除通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody DeleteDto dto)
    {
        Long []ids=DelParaValid.validBatchDel(dto);
        int rowsAffect=noticeService.deleteNoticeByIds(ids,getUsername());
        return rowsAffect>0?success():error("删除失败");
    }

    /**
     * 分页查询
     */
    @ApiOperation("获取通知公告列表")
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @Log(title = "通知公告分页查询", businessType = BusinessType.OTHER)
    @PostMapping("/list")
    public AjaxResult list(@RequestBody SysNoticeQuery notice)
    {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return success(getDataTable(list));
    }

    /**
     * 根据id获取详细信息
     */
    @ApiOperation("根据id获取详细信息")
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @Log(title = "根据id获取通知公告", businessType = BusinessType.OTHER)
    @GetMapping("/getById")
    public AjaxResult getInfo(Long id)
    {
        return success(noticeService.selectNoticeById(id));
    }
}
